package com.xiaoyi.intentsdklibrary.SDK.Search;

import android.app.AppOpsManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.os.BatteryManager;
import android.os.Binder;
import android.os.Build;
import android.provider.Settings;
import android.view.Display;
import android.view.WindowManager;
import com.xiaoyi.intentsdklibrary.Bean.AppInfoBean;
import com.xiaoyi.intentsdklibrary.Bean.PermissionBean;
import com.xiaoyi.intentsdklibrary.FlashUtils;
import com.xiaoyi.intentsdklibrary.StateBarUtil;
import com.xiaoyi.intentsdklibrary.Utils.NoticUtil;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

public class SearchSDK {
    private static final SearchSDK ourInstance = new SearchSDK();
    private FlashUtils mFlashUtils;

    public enum RingdModeType {
        MODEL_QUIET,
        MODEL_RING,
        MODEL_VIBRATE
    }

    public interface onIMEIListener {
        void result(String str);
    }

    public static SearchSDK getInstance() {
        return ourInstance;
    }

    private SearchSDK() {
    }

    public boolean getWifiState(Context context) {
        return true;
    }

    public boolean getBlueToothState(Context context) {
//        BluetoothAdapter adapter = ((BluetoothManager) context.getSystemService("bluetooth")).getAdapter();
//        if (adapter == null || !adapter.isEnabled()) {
//            return false;
//        }
        return true;
    }

    public boolean getFlashState(Context context) {
        if (this.mFlashUtils == null) {
            this.mFlashUtils = new FlashUtils(context);
        }
        return this.mFlashUtils.getState();
    }

    public RingdModeType getRingModel(Context context) {
        int ringerMode = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).getRingerMode();
        if (ringerMode == 0) {
            return RingdModeType.MODEL_QUIET;
        }
        if (ringerMode != 1) {
            return RingdModeType.MODEL_VIBRATE;
        }
        return RingdModeType.MODEL_RING;
    }

    public boolean getFlowState(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Boolean bool = false;
        try {
            bool = (Boolean) connectivityManager.getClass().getMethod("getMobileDataEnabled").invoke(connectivityManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bool.booleanValue();
    }

    public boolean getGPSState(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return locationManager.isProviderEnabled("gps") || locationManager.isProviderEnabled("network");
    }

    public boolean getWifiOpState(Context context) {
        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (((Integer) wifiManager.getClass().getDeclaredMethod("getWifiApState", new Class[0]).invoke(wifiManager, new Object[0])).intValue() == ((Integer) wifiManager.getClass().getDeclaredField("WIFI_AP_STATE_ENABLED").get(wifiManager)).intValue()) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean getNFCState(Context context) {
        NfcAdapter defaultAdapter = ((NfcManager) context.getSystemService("nfc")).getDefaultAdapter();
        return defaultAdapter != null && defaultAdapter.isEnabled();
    }

    public boolean getFlyState(Context context) {
        return !Settings.System.getString(context.getContentResolver(), "airplane_mode_on").equals("0");
    }

    public boolean getScreenStata(Context context) {
        try {
            if (Settings.System.getInt(context.getContentResolver(), "accelerometer_rotation") == 1) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public ArrayList<AppInfoBean> getAllApp(Context context) {
        ArrayList<AppInfoBean> arrayList = new ArrayList<>();
        List<PackageInfo> installedPackages = context.getPackageManager().getInstalledPackages(0);
        for (int i = 0; i < installedPackages.size(); i++) {
            PackageInfo packageInfo = installedPackages.get(i);
            arrayList.add(new AppInfoBean(packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString(), packageInfo.packageName, packageInfo.versionName, packageInfo.versionCode, packageInfo.applicationInfo.loadIcon(context.getPackageManager())));
        }
        return arrayList;
    }

    private List<PermissionBean> getAllPermission(Context context) {
        ArrayList arrayList = new ArrayList();
        PackageManager packageManager = context.getPackageManager();
        try {
            String[] strArr = packageManager.getPackageInfo(context.getPackageName(), 4096).requestedPermissions;
            if (strArr != null) {
                for (String str : strArr) {
                    arrayList.add(new PermissionBean(str, packageManager.checkPermission(str, context.getPackageName()) == 0));
                }
            }
            return arrayList;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (Throwable unused) {
        }
        return arrayList;
    }

    public boolean checkNoticPermission(Context context) {
        return NoticUtil.isNotificationEnabled(context);
    }

    public boolean checkFloatPermission(Context context) {
        if (Build.VERSION.SDK_INT >= 19) {
            AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
            try {
                Class.forName(appOpsManager.getClass().getName());
                if (((Integer) appOpsManager.getClass().getDeclaredMethod("checkOp", Integer.TYPE, Integer.TYPE, String.class).invoke(appOpsManager, 24, Integer.valueOf(Binder.getCallingUid()), context.getPackageName())).intValue() == 0) {
                    return true;
                }
                return false;
            } catch (Exception unused) {
            }
        }
        return true;
    }

    public String getSignature(Context context) {
        try {
            return encryptionMD5(context.getPackageManager().getPackageInfo(context.getPackageName(), 64).signatures[0].toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String encryptionMD5(byte[] bArr) {
        StringBuffer stringBuffer = new StringBuffer();
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.reset();
            instance.update(bArr);
            byte[] digest = instance.digest();
            for (int i = 0; i < digest.length; i++) {
                if (Integer.toHexString(digest[i] & -1).length() == 1) {
                    stringBuffer.append("0");
                    stringBuffer.append(Integer.toHexString(digest[i] & -1));
                } else {
                    stringBuffer.append(Integer.toHexString(digest[i] & -1));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

    public String getVersionName(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public int getVersionCode(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public int getScreenWith(Context context) {
        Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        defaultDisplay.getHeight();
        return defaultDisplay.getWidth();
    }

    public int getScreenHeight(Context context) {
        Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int height = defaultDisplay.getHeight();
        defaultDisplay.getWidth();
        return height;
    }

    public String getPhoneModel() {
        return Build.MODEL;
    }

    public String getPhoneBrand() {
        return Build.BRAND;
    }

    public int getStaeBarHeight(Context context) {
        return StateBarUtil.getStatusBarHeight(context);
    }

    public String getPastText(Context context) {
        return ((ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE)).getPrimaryClip().getItemAt(0).getText().toString();
    }

    public boolean isCharging(Context context) {
        return context.registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED")).getIntExtra("plugged", -1) != 0;
    }

    public int getMaxVolume(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.getStreamVolume(3);
        return audioManager.getStreamMaxVolume(3);
    }

    public int getCurrentVolume(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int streamVolume = audioManager.getStreamVolume(3);
        audioManager.getStreamMaxVolume(3);
        return streamVolume;
    }

    private int getSystemBrightness(Context context) {
        try {
            return Settings.System.getInt(context.getContentResolver(), "screen_brightness");
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
    }

    private int getBatteryNum(Context context) {
        if (Build.VERSION.SDK_INT >= 21) {
            return ((BatteryManager) context.getSystemService(Context.BATTERY_SERVICE)).getIntProperty(4);
        }
        Intent registerReceiver = new ContextWrapper(context).registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        return (registerReceiver.getIntExtra("level", -1) * 100) / registerReceiver.getIntExtra("scale", -1);
    }
}
